import * as XLSX from "xlsx";
import fs from "file-saver";
import {
  cloneDeep
} from "lodash-es";
import {
  excel
} from "../api/excel";


const s2ab = (s) => {
  var buf;
  if (typeof ArrayBuffer !== "undefined") {
    buf = new ArrayBuffer(s.length);
    var view = new Uint8Array(buf);
    for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xff;
    return buf;
  } else {
    buf = new Array(s.length);
    for (var i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xff;
    return buf;
  }
};

// 自动分析dom元素导出excel
export function excelExport(table, filename) {
  // workbook,
  const wb = XLSX.utils.table_to_book(table);

  /* Export to file (start a download) */
  const defaultCellStyle = {
    font: {
      name: "Verdana",
      sz: 13,
      color: "FF00FF88"
    },
    fill: {
      fgColor: {
        rgb: "FFFFAA00"
      }
    },
  };
  const wopts = {
    bookType: "xlsx",
    bookSST: false,
    type: "binary",
    cellStyle: true,
    defaultCellStyle: defaultCellStyle,
    showGridLines: false,
  };
  const wbout = XLSX.write(wb, wopts);
  const blob = new Blob([s2ab(wbout)], {
    type: "application/octet-stream"
  });
  fs.saveAs(blob, filename + ".xlsx");
}

// 使用数据导出excel
export function excelExport2(data, headers, filename) {
  const json = cloneDeep(data);

  json.forEach((item) => {
    for (let key in item) {
      if (headers.hasOwnProperty(key)) {
        item[headers[key]] = item[key];
      }
      delete item[key];
    }
  });

  // excel 对象
  const wb = XLSX.utils.book_new();
  const ws = XLSX.utils.json_to_sheet(json, {
    header: Object.values(headers)
  });
  wb.SheetNames.push(filename);
  wb.Sheets[filename] = ws;
  const defaultCellStyle = {
    font: {
      name: "Verdana",
      sz: 13,
      color: "FF00FF88"
    },
    fill: {
      fgColor: {
        rgb: "FFFFAA00"
      }
    },
  };
  const wopts = {
    bookType: "xlsx",
    bookSST: false,
    type: "binary",
    cellStyle: true,
    defaultCellStyle: defaultCellStyle,
    showGridLines: false,
  };
  const wbout = XLSX.write(wb, wopts);
  const blob = new Blob([s2ab(wbout)], {
    type: "application/octet-stream"
  });
  fs.saveAs(blob, filename + ".xlsx");
}

// excel导入
export function excelImport(file, output) {
  const type = file.name.split(".")[1];
  const fileType = ["xlsx", "xlc", "xlm", "xls", "xlt"].some(
    (item) => item === type
  );
  if (!fileType) {
    ElMessage.error("格式错误！请重新选择");
    return;
  }

  const reader = new FileReader(file);
  reader.readAsArrayBuffer(file, "utf-8");
  reader.onloadend = function (e) {
    const data = e.target.result;
    /* Parse file */
    // workbook
    const wb = XLSX.read(data, {
      type: "buffer",
    });
    // worksheet
    const ws = wb.Sheets[wb.SheetNames[0]];
    const json = XLSX.utils.sheet_to_json(ws);
    const htmlstr = XLSX.utils.sheet_to_html(ws);
    /* Generate HTML */
    output.innerHTML = htmlstr;
    console.log(json);
    const lists = [];
    //console.log(ws);
    // 从解析出来的数据中提取相应的数据
    json.forEach((item) => {
      //读出来的excel表里的数据放在lists数组里
      lists.push({
        proname: item["名称"],
        img1: item["图片链接"],
        category: item["分类"],
        originprice: item["价格"],
        discount: item["折扣"],
        sales: item["销量"],
      });
    });

    excel(JSON.stringify(lists)).then(res => {
      console.log(res)
    })
    console.log(lists)
    // console.log(htmlstr)
    return lists;

  };
}